/**************************************************************************
 *   Copyright (C) 2008 by Chris Vickery   *
 *   chrisinajar@gmail.com   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#include "graphicalobject.h"

#include <math.h>

#include <Ogre.h>
#include <QDebug>

#define PI 3.14159265

GraphicalObject::GraphicalObject(QObject *parent)
		: QObject(parent)
{
	Ogre::Root* root = &Ogre::Root::getSingleton();
	sceneNode = root->getSceneManager("Default SceneManager")->getSceneNode("ShipNode");
}

GraphicalObject::~GraphicalObject()
{
}

void GraphicalObject::setX(double x)
{
	Ogre::Vector3 vec = sceneNode->getPosition();
	vec.x = x;
	sceneNode->setPosition(vec);
}

void GraphicalObject::setY(double y)
{
	Ogre::Vector3 vec = sceneNode->getPosition();
	vec.y = y;
	sceneNode->setPosition(vec);
}

void GraphicalObject::setDirection(double d)
{
	Ogre::Quaternion dir = sceneNode->getOrientation();
	double x = 0;
	double y = 0;

	while (d < 0)
		d += 360;

	while (d > 360)
		d -= 360;

	if (d > 270)
	{
		// top left
		d -= 270;
		x = -sin(d * PI / 180);
		y = cos(d * PI / 180);
	}
	else if (d > 180)
	{
		// bottom left
		d -= 180;
		y = -sin(d * PI / 180);
		x = -cos(d * PI / 180);
	}
	else if (d > 90)
	{
		// bottom right
		d -= 90;
		x = sin(d * PI / 180);
		y = -cos(d * PI / 180);
	}
	else
	{
		// top left
		y = sin(d * PI / 180);
		x = cos(d * PI / 180);
	}
	dir.x = x;
	dir.y = y;
	sceneNode->setOrientation(dir);

}

double GraphicalObject::getX()
{
}

double GraphicalObject::getY()
{
}

double GraphicalObject::getDirection()
{
}




